﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomainModel.Concrete;
using DomainModel.Entities;
using System.Web.Security;
namespace WebUI.Controllers
{
    [Authorize]
    public class AlbumController : Controller
    {
        SqlAlbumRepository _albumRepository;
        public AlbumController()
        {
            _albumRepository = new SqlAlbumRepository();
        }
        #region Private Region
        private ActionResult HandleMyError(Exception ex)
        {
            var modelErr = new ErrorHandling(ex);
            return View("Error", modelErr);
        }
        private bool ValidateImageFile(HttpPostedFileBase pictureFile)
        {
            if (pictureFile == null || pictureFile.ContentLength == 0)
            {
                return true;
            }
            if (pictureFile.ContentType == "image/jpeg")
            {
                return true;
            }
            if (pictureFile.ContentType == "image/jpg")
            {
                return true;
            }
            if (pictureFile.ContentType == "image/png")
            {
                return true;
            }
            if (pictureFile.ContentType == "image/bmp")
            {
                return true;
            }
            if (pictureFile.ContentType == "image/gif")
            {
                return true;
            }

            return false;
        }
        #endregion
        #region Action Results

        public ActionResult Index(int id) // artist id
        {
            ViewData["referringUrl"] = Request.UrlReferrer;
            return View(_albumRepository.GetAll().Where(a => a.ArtistID == id));
        }

        public ActionResult Details(int id)
        {
            ViewData["referringUrl"] = Request.UrlReferrer;
            ViewData["AlbumID"] = id;
            return View(_albumRepository.GetById(id));
        }

        public ActionResult Create(int id)
        {
            ViewData["referringUrl"] = Request.UrlReferrer;

            Album album = new Album();
            album.LargePictureID = 0;
            album.SmallPictureID = 0;
            album.LastUpdate = System.DateTime.Now;
            album.ArtistID = id;

            return View(album);
        }

        [HttpPost]
        public ActionResult Create(Album album, HttpPostedFileBase pictureFile, string referringUrl)
        {

            try
            {
                if (!ValidateImageFile(pictureFile))
                {
                    return View(album);
                }

                album.UserName = Membership.GetUser().UserName;
                album.LastUpdate = DateTime.Now;
                album.IsApproved = false;
                album.HitCount = 0;

                _albumRepository.Insert(album);
                string _strPictureFileName = album.ArtistID.ToString() + "_" + album.AlbumID.ToString();
                if (pictureFile.ContentLength > 0)
                {
                    PictureController pictureController = new PictureController();
                    /*
                     Picture picture = new Picture();
                     picture.PictureFileName = album.AlbumID.ToString();
                     album.PictureID = pictureController.SavePictureFile(picture, pictureFile);
                     */

                    Picture pictureLarge = new Picture();
                    pictureLarge.PictureCaption = "Large-Image-of-" + album.AlbumName;
                    pictureLarge.PictureFileName = _strPictureFileName + "_Large_Album";
                    album.LargePictureID = pictureController.SavePictureFile(pictureLarge, pictureFile, 320, 460);

                    //Picture pictureLarge = new Picture();
                    //pictureLarge.PictureFileName = album.Artist.FileHostID.ToString() + "_" + "Large_" + album.ArtistID.ToString() + "_" + album.AlbumID.ToString();
                    //album.LargePictureID = pictureController.SavePictureFile(pictureLarge, pictureFile, 320, 460);


                    Picture pictureSmall = new Picture();
                    pictureSmall.PictureCaption = "Small-Image-of-" + album.AlbumName;
                    pictureSmall.PictureFileName = _strPictureFileName + "_Small_Album";
                    album.SmallPictureID = pictureController.SavePictureFile(pictureSmall, pictureFile, 80, 80);

                    //Picture pictureSmall = new Picture();
                    //pictureSmall.PictureFileName = album.Artist.FileHostID.ToString() + "_" + "Small_" + album.ArtistID.ToString() + "_" + album.AlbumID.ToString();
                    //album.SmallPictureID = pictureController.SavePictureFile(pictureSmall, pictureFile, 80, 80);

                    _albumRepository.Update(album);
                }


                if (string.IsNullOrEmpty(referringUrl))
                {
                    return RedirectToAction("Details", "Artist", new { id = album.ArtistID });
                }
                else
                {
                    return Redirect(referringUrl);
                }
            }
            catch (Exception ex)
            {
                return HandleMyError(ex);
            }
        }

        public ActionResult Edit(int id)
        {
            ViewData["referringUrl"] = Request.UrlReferrer;
            return View(_albumRepository.GetById(id));
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection, HttpPostedFileBase pictureFile, string referringUrl)
        {
            try
            {
                Album album = _albumRepository.GetById(id);
                TryUpdateModel(album, null, null, null, collection);

                if (pictureFile != null && pictureFile.ContentLength > 0)
                {
                    PictureController pictureController = new PictureController();
                    /*
                     if (album.Picture == null)
                     {
                         album.Picture = new Picture();
                         album.Picture.PictureFileName = album.ArtistID.ToString();
                     }
                     album.PictureID = pictureController.SavePictureFile(album.Picture, pictureFile);
                     */

                    if (album.LargePicture == null)
                    {
                        album.LargePicture = new Picture();
                        album.LargePicture.PictureFileName = album.Artist.FileHostID.ToString() + "_" + "Large_" + album.ArtistID.ToString() + "_" + album.AlbumID.ToString();
                    }
                    album.LargePictureID = pictureController.SavePictureFile(album.LargePicture, pictureFile, 320, 460);

                    if (album.SmallPicture == null)
                    {
                        album.SmallPicture = new Picture();
                        album.SmallPicture.PictureFileName = album.Artist.FileHostID.ToString() + "_" + "Small_" + album.ArtistID.ToString() + "_" + album.AlbumID.ToString();
                    }
                    album.SmallPictureID = pictureController.SavePictureFile(album.SmallPicture, pictureFile, 80, 80);

                }
                _albumRepository.Update(album);

                if (string.IsNullOrEmpty(referringUrl))
                {
                    //return RedirectToAction("Index", "Home");
                    return RedirectToAction("Details", "Artist", new { id = album.ArtistID });
                }
                else
                {
                    return Redirect(referringUrl);
                }

            }
            catch (Exception ex)
            {
                return HandleMyError(ex);
            }
        }

        public ActionResult Delete(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                _albumRepository.Delete(_albumRepository.GetById(id));
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return HandleMyError(ex);
            }
        }
        #endregion
    }
}
